/**
 * Copyright(c) by JNet Smartware Group.
 * All rights reserved. Not distribution or redistribution without
 * any permission of JNet Smartware Group.
 * 
 * @author thuc
 * 2010/03/15
 */
package vn.smartware.co.server.action;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import vn.smartware.co.framework.action.BaseAction;
import vn.smartware.co.framework.bean.CommonItemBean;
import vn.smartware.co.framework.bean.MenuDTO;
import vn.smartware.co.framework.bean.AddressDTO;
import vn.smartware.co.framework.bean.UserDTO;
import vn.smartware.co.framework.bean.ProgramDTO;
import vn.smartware.co.framework.bean.LoginDTO;
import vn.smartware.co.framework.config.SystemConfig;
import vn.smartware.co.framework.exception.ServerException;
import vn.smartware.co.framework.form.ActionForm;
import vn.smartware.co.framework.form.BaseForm;
import vn.smartware.co.server.dao.factory.DaoFactory;
import vn.smartware.co.server.dao.initialize.InitializeDAO;

/**
 * LoginAction
 * @author thuctt
 */
public class InitializeAction extends BaseAction {

	private static Log log = LogFactory.getLog(InitializeAction.class);
	
	/**
	 * get menu
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm getMenu(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		BaseForm form = (BaseForm)aForm;
		InitializeDAO dao = DaoFactory.createInitializeDAO();
		Connection conn = getConnection();
		
		try {
			log.info("Get menu start....");
			
			LoginDTO user = form.getUserDTO();
			ArrayList<MenuDTO> list = dao.getMenu(user, conn);
			
			if (!list.isEmpty()){
				for (int i = 0; i < list.size(); i++) {
					MenuDTO menu = (MenuDTO)list.get(i);
					ArrayList<ProgramDTO> prglist = dao.getPrgList(menu, user, conn);
					menu.setPrograms(prglist);
				}
			}
			form.setMenu(list);
			
			log.info("Get menu end.");
			
		} catch (ServerException e) {
			log.error("InitializeAction getMenu ServerException.", e);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * get only menu
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm getMenuListWithoutAuth(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		BaseForm form = (BaseForm)aForm;
		InitializeDAO dao = DaoFactory.createInitializeDAO();
		Connection conn = getConnection();
		
		try {
			
			LoginDTO user = form.getUserDTO();
			ArrayList<MenuDTO> list = dao.getMenuListWithoutAuth(user, conn);
						
			form.setMenu(list);
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * regist menu list
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm registMenu(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		BaseForm form = (BaseForm)aForm;
		LoginDTO user = (LoginDTO)form.getUserDTO();
		InitializeDAO dao = DaoFactory.createInitializeDAO();
		Connection conn = getConnection();
		
		try {
			ArrayList<MenuDTO> ls = form.getMenu();
			for (int i = 0; i < ls.size(); i++) {
				MenuDTO dto = (MenuDTO)ls.get(i);
				if (dto.getMenuId() == -1){
					dao.createNewMenu(dto, user, conn);
					dto.setMenuId(dao.getMaxMenuId(conn));
					dao.createMenuAuthority(dto, user, conn);
				}else{
					dao.updateMenu(dto, user, conn);
				}
			}
			
			commit(conn);
			
		} catch (ServerException e) {
			log.error("InitializeAction registMenu ServerException.", e);
			rollback(conn);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * delete Menu
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm deleteMenu(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		BaseForm form = (BaseForm)aForm;
		InitializeDAO dao = DaoFactory.createInitializeDAO();
		Connection conn = getConnection();
		
		try {
			ArrayList<MenuDTO> ls = form.getMenu();
			for (int i = 0; i < ls.size(); i++) {
				MenuDTO dto = (MenuDTO)ls.get(i);
				dao.deleteMenu(dto, conn);
				dao.deleteAuth(dto, conn);
			}
			
			commit(conn);
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			rollback(conn);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * delete User
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm deleteUserAction(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		BaseForm form = (BaseForm)aForm;
		InitializeDAO dao = DaoFactory.createInitializeDAO();
		Connection conn = getConnection();
		
		try {
			ArrayList<UserDTO> ls = form.getUserList();
			for (int i = 0; i < ls.size(); i++) {
				UserDTO dto = (UserDTO)ls.get(i);
				dao.deleteUser(dto, conn);
				dao.deleteAuthOfUser(dto, conn);
				AddressDTO addr = dto.getAddress();
				if (null != addr){
					dao.deleteAddress(addr, conn);
				}
			}
			
			commit(conn);
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			rollback(conn);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * Clear all rows in table ACTIONLOG
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm clearLog(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		BaseForm form = (BaseForm)aForm;
		InitializeDAO dao = DaoFactory.createInitializeDAO();
		Connection conn = getConnection();
		
		try {
			dao.clearLog(conn);
						
			commit(conn);
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			rollback(conn);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * get initialize items data for combobox
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm getComboInitItems(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		BaseForm form = (BaseForm)aForm;
		InitializeDAO dao = DaoFactory.createInitializeDAO();
		Connection conn = getConnection();
		
		try {
			log.info("getComboInitItems start....");
			
			List<CommonItemBean> result = dao.getComboInitItems(form.getMap(), conn);
			form.setItemValueBeanList(result);
			
			log.info("getComboInitItems end.");
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * get program list
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm getPrograms(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		BaseForm form = (BaseForm)aForm;
		InitializeDAO dao = DaoFactory.createInitializeDAO();
		Connection conn = getConnection();
		
		try {
			log.info("getPrograms start....");
			
			LoginDTO user = (LoginDTO)form.getUserDTO();
			
			form.setPrgList(dao.getPrograms(form, user, conn));
			
			log.info("getPrograms finish.");
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * get program list of current login user
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm getAuthPrograms(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		BaseForm form = (BaseForm)aForm;
		InitializeDAO dao = DaoFactory.createInitializeDAO();
		Connection conn = getConnection();
		
		try {
			log.info("getAuthPrograms start....");
			
			form.setPrgList(dao.getAuthPrograms(form.getUserID(), conn));
			
			log.info("getAuthPrograms finish.");
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * get menu
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm getMenuList(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		BaseForm form = (BaseForm)aForm;
		InitializeDAO dao = DaoFactory.createInitializeDAO();
		Connection conn = getConnection();
		
		try {
			log.info("getMenuList start....");
			
			LoginDTO user = (LoginDTO)form.getUserDTO();
			form.setMenu(dao.getMenu(user, conn));
			
			log.info("getMenuList end.");
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * getAvailableMenu
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm getAvailableMenu(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		BaseForm form = (BaseForm)aForm;
		InitializeDAO dao = DaoFactory.createInitializeDAO();
		Connection conn = getConnection();
		
		try {
			log.info("getAvailableMenu start....");
			
			form.setMenu(dao.getAvailableMenu(conn));
			
			log.info("getAvailableMenu end.");
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * save registered program list
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm saveAction(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		BaseForm form = (BaseForm)aForm;
		InitializeDAO dao = DaoFactory.createInitializeDAO();
		Connection conn = getConnection();
		
		try {
			log.info("saveAction start....");
			
			LoginDTO user = (LoginDTO)form.getUserDTO();
			ArrayList<ProgramDTO> list = form.getPrgList();
			for (int i = 0; i < list.size(); i++) {
				ProgramDTO prg = (ProgramDTO)list.get(i);
				dao.registerProgram(prg, user, conn);
			}
			//
			commit(conn);
			
			log.info("saveAction end.");
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			rollback(conn);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * get user list
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm getUsers(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		BaseForm form = (BaseForm)aForm;
		InitializeDAO dao = DaoFactory.createInitializeDAO();
		Connection conn = getConnection();
		
		try {
			log.info("getUsers start....");
			
			LoginDTO user = (LoginDTO)form.getUserDTO();
			
			form.setUserList(dao.getUsers(form, user, conn));
			
			log.info("getUsers finish.");
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * save registered user list
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm saveUsersAction(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		BaseForm form = (BaseForm)aForm;
		InitializeDAO dao = DaoFactory.createInitializeDAO();
		Connection conn = getConnection();
		
		try {
			log.info("saveUsersAction start....");
			
			LoginDTO loginUser = (LoginDTO)form.getUserDTO();
			ArrayList<UserDTO> list = form.getUserList();
			for (int i = 0; i < list.size(); i++) {
				UserDTO user = (UserDTO)list.get(i);
				if (user.getUserId() == -1){//register new
					AddressDTO addr = user.getAddress();
					if (null != addr){
						dao.insertAddress(addr, loginUser, conn);
						addr.setAddressCd(dao.getMaxAddressCd(addr, loginUser, conn));
					}
					dao.registerLoginInfo(user, loginUser, conn);
					user.setUserId(dao.getMaxUserID(conn));
					
				}else{//update
					AddressDTO addr = user.getAddress();
					if (null != addr){
						if (addr.getAddressCd() == -1){
							dao.insertAddress(addr, loginUser, conn);
							addr.setAddressCd(dao.getMaxAddressCd(addr, loginUser, conn));
						}else{
							dao.updateAddress(addr, loginUser, conn);
						}
					}
					dao.updateLoginUser(user, loginUser, conn);
				}
			}
			//
			commit(conn);
			
			log.info("saveUsersAction end.");
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			rollback(conn);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * save programs having authoritied
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm saveAuthProgramsAction(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		BaseForm form = (BaseForm)aForm;
		InitializeDAO dao = DaoFactory.createInitializeDAO();
		Connection conn = getConnection();
		
		try {
			log.info("saveAuthProgramsAction start....");
			
			ArrayList<ProgramDTO> list = form.getPrgList();
			dao.deleteAllPrgsOfUser(form, conn);
			for (int i = 0; i < list.size(); i++) {
				ProgramDTO prg = (ProgramDTO)list.get(i);
				dao.registerAuth(prg, form.getUserID(), conn);
			}
			//
			commit(conn);
			
			log.info("saveAuthProgramsAction end.");
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			rollback(conn);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
}
